package org.ravey.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PluginInfo {
	protected String PluginUUID;
	protected String PluginTitle;
	protected String PluginAuthor;
	protected String PluginMainClass;
	protected File PluginJar;
	protected int KVersion;
	
	/**
	 * Create a new plugin information class
	 * 
	 * @param UUID the unique identifier for this plugin
	 * @param Title The Title of the plugin
	 * @param Author The Author of the Plugin
	 * @param MainClass The name of the central plugin class (class that implements IPlugin)
	 * @param JarPath The path to the jar file containing this plugin
	 * @param KernelVersion The minimum version of the application kernel required to run the plugin
	 */
	public PluginInfo(String UUID, String Title, String Author, String MainClass, String JarPath, int KernelVersion){
		PluginUUID = UUID;
		PluginTitle = Title;
		PluginAuthor = Author;
		PluginMainClass = MainClass;
		PluginJar = new File(JarPath);
		KVersion = KernelVersion;
	}
	
	/**
	 * Load information from a file (convenience alias)
	 * @param InfoFilePath The path of the information file
	 * @throws IOException
	 */
	public void LoadInfo(String InfoFilePath) throws IOException{
		LoadInfo(new File(InfoFilePath));
	}
	
	/**
	 * Load information from a file
	 * @param InfoFile a file object representing the information file
	 * @throws IOException
	 */
	public void LoadInfo(File InfoFile) throws IOException{
		
		//create an empty properties object
		Properties info = new Properties();
		//create an input stream from the provided file
		FileInputStream istream = new FileInputStream(InfoFile);
		//load the properties from the file
		info.load(istream);
		//close the stream
		istream.close();
		
		//load the info into the class
		PluginUUID = info.getProperty("plugin.uuid");
		PluginTitle = info.getProperty("plugin.title");
		PluginAuthor = info.getProperty("plugin.author");
		PluginMainClass = info.getProperty("plugin.mainclass");
		PluginJar = new File(info.getProperty("plugin.jarpath"));
		KVersion = Integer.parseInt(info.getProperty("plugin.minimumkernel"));
	}
	public void SaveInfo(String InfoFilePath) throws IOException{
		SaveInfo(new File(InfoFilePath));
	}
	
	public void SaveInfo(File InfoFile) throws IOException{
		
		//create an empty properties object
		Properties info = new Properties();

		//set the properties information to their respective values
		info.setProperty("plugin.uuid", PluginUUID);
		info.setProperty("plugin.title", PluginTitle);
		info.setProperty("plugin.author", PluginAuthor);
		info.setProperty("plugin.mainclass", PluginMainClass);
		info.setProperty("plugin.jarpath", PluginJar.getAbsolutePath());
		info.setProperty("plugin.minimumkernel", String.valueOf(KVersion));
		
		//open an output stream
		FileOutputStream fout = new FileOutputStream(InfoFile);
		//write the data to it
		info.store(fout, "");
		//close the stream
		fout.close();
	}
	
	public String GetID(){ return PluginUUID; }
	
	/**
	 * Get the plugin title
	 * 
	 * @return The full name of the plugin described
	 */
	public String GetTitle(){ return PluginTitle; }
	/**
	 * Get the plugin author name
	 * @return
	 */
	public String GetAuthor(){ return PluginAuthor; }
	/**
	 * Get the name of the main class as a string
	 * @return
	 */
	public String GetMainClass(){ return PluginMainClass; }
	/**
	 * Get The absolute path of the plugin in the filesystem
	 * @return
	 */
	public String GetJarPath(){ return PluginJar.getAbsolutePath(); }
	/**
	 * Get the minimum version of the kernel needed to run this plugin
	 * @return
	 */
	public int GetKernelVersion(){ return KVersion; }
}
